package com.mofang.powerdekorhelper.adapter;

import android.content.Context;
import android.view.View;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseViewHolder;
import com.mofang.powerdekorhelper.model.Order;

import java.util.List;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class OrderAdapter extends BaseRecycleViewAdapter {

    IOrderClick orderClick;

    public OrderAdapter(List<?> data, Context mContext, int layoutId) {
        super(data, mContext, layoutId);
    }

    @Override
    protected <T> void convert(BaseViewHolder holder, T bean, int position) {

        final Order.Data data = (Order.Data) bean;

        if (data.getActivityCode() != null && !data.getActivityCode().equals("")) {
            holder.setText(R.id.order_code_tv, data.getActivityCode());
        } else {
            holder.setText(R.id.order_code_title_tv, "订单编号:");
            if (data.getOrderCode() != null && !data.getOrderCode().equals("")) {
                holder.setText(R.id.order_code_tv, data.getOrderCode());
            } else {
                holder.setText(R.id.order_code_tv, "无");
            }
        }

        int orderStateCode = data.getOrderState();

        String orderStateInfo = "";
        switch (orderStateCode) {
            case 1:
                orderStateInfo = "物流";
                break;
            case 2:
                orderStateInfo = "安装";
                break;
            case 3:
                orderStateInfo = "售后";
                break;
            case 4:
                orderStateInfo = "关闭";
                break;
        }
        holder.setText(R.id.order_state_tv, orderStateInfo);

        holder.setText(R.id.order_time_tv, "订单时间：" + data.getCreateTime());

        holder.setText(R.id.order_customer_name_tv, data.getCustomerName());
        holder.setText(R.id.order_contact_information_tv, data.getCustomerPhone());
        holder.setText(R.id.order_product_type_tv, data.getProductModel());
        holder.setText(R.id.order_amount_tv, "¥ " + data.getOrderPrice());

        holder.itemView.findViewById(R.id.order_detial_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orderClick.orderDetail(data.getOrderId());
            }
        });

        if (data.getActivityId() == null || data.getCustomerId() == null) {
            holder.setViewVisiable(R.id.order_refresh_imv, View.GONE);
        }

        holder.itemView.findViewById(R.id.order_refresh_imv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                orderClick.orderRefresh(data.getOrderId(), data.getOrderCode(), data.getActivityId(), data.getCustomerId());
            }
        });

        holder.itemView.findViewById(R.id.order_close_order_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                orderClick.orderClose(data.getOrderId(), data.getOrderState());
            }
        });

        if (data.getOrderState() == 4) {
            holder.setViewVisiable(R.id.order_close_order_tv, View.GONE);
        } else {
            holder.setViewVisiable(R.id.order_close_order_tv, View.VISIBLE);
        }
    }

    public void setOrderClick(IOrderClick orderClick) {
        this.orderClick = orderClick;
    }

    public interface IOrderClick {

        public void orderRefresh(int orderId, String orderCode, int activityId, int customerId);

        public void orderDetail(int orderId);

        public void orderClose(int orderId, int orderState);
    }

    public void addData(List<Order.Data> datas) {
        mData.addAll(datas);
        notifyDataSetChanged();
    }
}
